<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        :class="{active: sorter==='weight_desc'}"
        @click="changeSorter('weight_desc')"
        >推荐</a
      >
      <a
        href="javascript:;"
          :class="{active: sorter===''}"
        @click="changeSorter('')"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>
    <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
    <!-- v-for循环列表-->
    <article-item :item="item" v-for="item in list" :key="item.id"></article-item>
</van-list>
  </div>
</template>

<script>
import { getArticels } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      current: 1,
      sorter: 'weight_desc',
      flag: true

    }
  },
  methods: {
    async onLoad () {
      const res = await getArticels({
        current: this.current,
        pageSize: 10,
        sorter: this.sorter

      })

      this.current++
      // 不能直接覆盖,要追加到数组后面
      this.list = this.list.concat(res.data.data.rows)
      this.flag = true
      // 要记得将loding改成false
      this.loading = false
      // 加载完成后一定要判断,自增后没有超出总的页数
      if (this.current > res.data.data.pageTotal) {
        // 说明没有更多数据了
        this.finished = true
      }
    },
    changeSorter (sorter) {
      if (!this.flag) return
      this.sorter = sorter
      this.list = []
      this.current = 1
      this.finished = false
      // this.loading = false
      this.flag = false
      this.loading = true
      // 重新发送请求
      this.onLoad()
    }
  },

  async created () {
  //   // 测试接口:发现请求是发送了, 但是需要传一个token
  //   const res = await getArticels({
  //     current: 1,
  //     pageSize: 10
  //   })
  //   //    console.log(res)
  //   this.list = res.data.data.rows
  }

}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}

</style>
